package com.ricemall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.server.ServerWebExchange;

/**
 * @Author cappuccino
 * @Date 2021-06-05 13:43
 */
@Configuration
public class RicemallCorsConfiguration {

    /**
     * reactive风格跨域配置
     * @return
     */
    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨越
        //1.1、允许哪些头信息跨域
        corsConfiguration.addAllowedHeader("*");
        //1.2、允许哪些请求方式跨域
        corsConfiguration.addAllowedMethod("*");
        //1.3、允许请求来源跨域
        corsConfiguration.addAllowedOrigin("*");
        //1.4、允许携带Cookie进行跨域
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }

}
